package com.seeyon.apps.seepkk.form.workflowevent;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.seeyon.apps.kk.form.workflowevent.KkBaseExtendPlug;
import com.seeyon.apps.kk.manager.FormDataBaseManager;
import com.seeyon.apps.seepkk.manager.SeepManager;
import com.seeyon.cap4.form.api.FormApi4Cap4;
import com.seeyon.cap4.form.bean.FormBean;
import com.seeyon.cap4.form.bean.FormDataMasterBean;
import com.seeyon.cap4.form.bean.FormFieldBean;
import com.seeyon.ctp.common.constants.ApplicationCategoryEnum;
import com.seeyon.ctp.workflow.event.WorkflowEventData;
public class SeepAutoToWordExtendPlug extends KkBaseExtendPlug {
	private static final Log log = LogFactory.getLog(SeepAutoToWordExtendPlug.class);
	private SeepManager seepManager;
	private FormApi4Cap4 formApi4Cap4;
	private FormDataBaseManager kkBaseFormManager;
	public void setSeepManager(SeepManager seepManager) {
		this.seepManager = seepManager;
	}
	public void setFormApi4Cap4(FormApi4Cap4 formApi4Cap4) {
		this.formApi4Cap4 = formApi4Cap4;
	}
	public void setKkBaseFormManager(FormDataBaseManager kkBaseFormManager) {
		this.kkBaseFormManager = kkBaseFormManager;
	}
	@Override
	public String getId() {
		// TODO Auto-generated method stub
		return "seepAutoToWordExtendPlug";
	}

	@Override
	public String getLabel() {
		// TODO Auto-generated method stub
		return "Word控件自动生成并转换为PDF";
	}

	public ApplicationCategoryEnum getAppName() {
		return ApplicationCategoryEnum.form;
	}
	//发起事件
    public void onStart(WorkflowEventData data){
    	log.info("seepkk-发起事件【Word控件自动生成并转换为PDF】触发...");
    	FormBean formBean = (FormBean) data.getBusinessData().get("formBean");
		FormDataMasterBean formDataBean = (FormDataMasterBean) data.getBusinessData().get("formDataBean");		
		Thread t = new Thread(new Runnable(){  
            public void run(){ 
            	try {
            		Thread.sleep(3000);
            		kkBaseFormManager.autoGenerateWord(data.getSummaryId(), formBean, formDataBean);
            		log.info("seepkk-自动生成word文件执行结束!");
        		} catch (Exception e) {
        			log.error("seepkk自动生成word文件异常：", e);
        		}
        		//将word附件转换为PDF并挂载到表单
        		Map<String, Object> fileMap=new HashMap<String, Object>();
        		for(FormFieldBean fieldBean : formBean.getAllFieldBeans()){
        			//判断控件是否为表单转word控件
        			if("1872888230778916558".equals(fieldBean.getInputType())){
        				log.info("seepkk-判断控件为表单转word控件，即将转换PDF...");
        				try {
        					Long subreference=formDataBean.getFieldValue(fieldBean.getName())!=null?Long.valueOf(formDataBean.getFieldValue(fieldBean.getName()).toString()):null;
        					Long pdfSubReference=seepManager.wordToPdf(subreference, data.getSummaryId());
        					if(pdfSubReference!=null){
        						FormFieldBean formFieldBean=formDataBean.getFormTable().getFieldBeanByDisplay(fieldBean.getDisplay()+"-PDF");
        						if(formFieldBean!=null){
        							formDataBean.addFieldValue(formFieldBean.getName(), pdfSubReference);
        						}
        					}
        				} catch (Exception e) {
        					log.error("seepkk转换PDF文件异常：", e);
        				} 
        			}
        		}
        		try {
        			formApi4Cap4.saveOrUpdateFormData(formDataBean, formBean.getId(), true);
        		} catch (Exception e) {
        			log.error("seepkk-PDF文件回填表单异常：", e);
        		}
            }});  
        t.start();
    }
  //处理后事件
    public void onFinishWorkitem(WorkflowEventData data){
    	FormBean formBean = (FormBean) data.getBusinessData().get("formBean");
		FormDataMasterBean formDataBean = (FormDataMasterBean) data.getBusinessData().get("formDataBean");
		try {
			kkBaseFormManager.autoGenerateWord(data.getSummaryId(), formBean, formDataBean);
		} catch (Exception e) {
			log.error("seepkk自动生成word文件异常：", e);
		}
		//将word附件转换为PDF并挂载到表单
		Map<String, Object> fileMap=new HashMap<String, Object>();
		for(FormFieldBean fieldBean : formBean.getAllFieldBeans()){
			//判断控件是否为表单转word控件
			if("1872888230778916558".equals(fieldBean.getInputType())){
				try {
					Long subreference=formDataBean.getFieldValue(fieldBean.getName())!=null?Long.valueOf(formDataBean.getFieldValue(fieldBean.getName()).toString()):null;
					Long pdfSubReference=this.seepManager.wordToPdf(subreference, data.getSummaryId());
					if(pdfSubReference!=null){
						FormFieldBean formFieldBean=formDataBean.getFormTable().getFieldBeanByDisplay(fieldBean.getDisplay()+"-PDF");
						if(formFieldBean!=null){
							formDataBean.addFieldValue(formFieldBean.getName(), pdfSubReference);
						}
					}
				} catch (Exception e) {
					log.error("seepkk转换PDF文件异常：", e);
				} 
			}
		}
		try {
			formApi4Cap4.saveOrUpdateFormData(formDataBean, formBean.getId(), true);
		} catch (Exception e) {
			log.error("seepkk-PDF文件回填表单异常：", e);
		}
    }

}